package com.hdu.clj.dncard

import android.app.Activity
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.view.View
import android.view.animation.TranslateAnimation
import androidx.appcompat.app.AlertDialog
import com.hdu.clj.dncard.databaseUtil.AllEquipDatabaseUtil
import com.hdu.clj.dncard.databaseUtil.HeroDatabaseUtil
import com.hdu.clj.dncard.databaseUtil.UserDatabaseUtil
import com.hdu.clj.dncard.util.GetEquip
import com.hdu.clj.dncard.util.LoadAndSave
import com.hdu.clj.dncard.util.RandomSkill
import kotlinx.android.synthetic.main.activity_map1.*

class Map1Activity : AppCompatActivity() {

    private val skilllist = arrayOf("card1","card2","card3","card4","card5","card6","card7","card8","card9")
    val exp = 500

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_map1)
        hprog.progress = hprog.progress + 100
        /** m1g1 m1g2 m1g3 表示地图1的三只怪物
         * 标记怪物是否存活
         */
        LoadAndSave().save("true","m1g1",this)
        LoadAndSave().save("true","m1g2",this)
        LoadAndSave().save("true","m1g3",this)
        val max_gwlife = 1000
        LoadAndSave().save("$max_gwlife","max_gwlife",this)
        val hero_max = LoadAndSave().load("maxlife",this)
        val cur_acc = LoadAndSave().load("userAcc",this)
        val cur_name = LoadAndSave().load("name",this)
        m1UserName.setText(cur_name)
        val cur_hid = HeroDatabaseUtil().findHero(this,cur_acc)
        if (cur_hid==1){
            m1HeroImage.setImageResource(R.drawable.hero0)
        }
        else{
            m1HeroImage.setImageResource(R.drawable.hero1)
        }
        m1UserName.setText(cur_name)
        htextb.setText("$hero_max/$hero_max")

        var cur_attack = LoadAndSave().load("hattack",this)
        var cur_widom = LoadAndSave().load("hwisdom",this)
        var cur_life = LoadAndSave().load("hlife",this)

        LoadAndSave().save("$max_gwlife","m1g1life",this)
        LoadAndSave().save("$max_gwlife","m1g2life",this)
        LoadAndSave().save("$max_gwlife","m1g3life",this)
        var m1g1_life = LoadAndSave().load("m1g1life",this)


        m1start.setOnClickListener {
            val flagg1 = LoadAndSave().load("m1g1",this)
            val flagg2 = LoadAndSave().load("m1g2",this)
            val flagg3 = LoadAndSave().load("m1g3",this)
            m1gprog.visibility = View.VISIBLE
            if (flagg1.equals("true")){
                LoadAndSave().save("m1g1","cur_fight",this)
                map1gw.setImageResource(R.drawable.m1g1)
                m1gprog.progress = 100
                m1gb.setText("生命值")
                mtextb.setText("$max_gwlife/$max_gwlife")
//                val cur_skill = RandomSkill().showSkill()
//                map1card1.setText(cur_skill[0])
//                map1card1.setTextColor(Color.parseColor(cur_skill[1]))
//                map1card2.setText(cur_skill[2])
//                map1card2.setTextColor(Color.parseColor(cur_skill[3]))
//                map1card3.setText(cur_skill[4])
//                map1card3.setTextColor(Color.parseColor(cur_skill[5]))
            }
            else if (flagg2.equals("true")){
                map1gw.visibility = View.VISIBLE
                map1gw.setImageResource(R.drawable.m1g2)
                m1gprog.progress = 100
                m1gb.setText("生命值")
                LoadAndSave().save("m1g2","cur_fight",this)

                mtextb.setText("$max_gwlife/$max_gwlife")

            }
            else if(flagg3.equals("true")){
                map1gw.visibility = View.VISIBLE
                map1gw.setImageResource(R.drawable.m1g3)
                m1gprog.progress = 100
                m1gb.setText("生命值")
                LoadAndSave().save("m1g3","cur_fight",this)

                mtextb.setText("$max_gwlife/$max_gwlife")
            }
            else{
                //胜利


            }
            val cur_skill = RandomSkill().showSkill(this)
            map1card1.setText(cur_skill[0])
            map1card1.setTextColor(Color.parseColor(cur_skill[1]))
            map1card2.setText(cur_skill[2])
            map1card2.setTextColor(Color.parseColor(cur_skill[3]))
            map1card3.setText(cur_skill[4])
            map1card3.setTextColor(Color.parseColor(cur_skill[5]))
            m1start.isClickable = false
            m1start.isEnabled = false
            m1start.setTextColor(Color.parseColor("#919AA2"))
            map1card1.visibility = View.VISIBLE
            map1card2.visibility = View.VISIBLE
            map1card3.visibility = View.VISIBLE
            map1card2.setBackgroundResource(R.drawable.card)
            map1card1.setBackgroundResource(R.drawable.card)
            map1card3.setBackgroundResource(R.drawable.card)
            map1card1.isClickable=true
            map1card1.isEnabled=true
            map1card2.isClickable=true
            map1card2.isEnabled=true
            map1card3.isClickable=true
            map1card3.isEnabled=true
        }

        map1card1.setOnClickListener {
            map1card1.visibility = View.INVISIBLE
            val res1 = RandomSkill().action(1,this)
            val cur_fight = LoadAndSave().load("cur_fight",this)
            if (res1<0){
                m1hurt.setText(res1.toString())
                Handler().postDelayed({
                    m1hurt.setText("")
                },400)
            }
            else{
                m1heroadd.setText("+$res1")
                Handler().postDelayed({
                    m1heroadd.setText("")
                },400)
            }

            cur_life = LoadAndSave().load("hlife",this)
            m1g1_life = LoadAndSave().load(cur_fight+"life",this)
            m1gprog.progress = (m1g1_life.toInt()*100)/max_gwlife
//            hprog.progress = (cur_life.toInt()*100)/(LoadAndSave().load("maxlife",this).toInt())
            hprog.progress = (cur_life.toInt()*100)/(LoadAndSave().load("maxlife",this).toInt())

            //显示血量数值
            htextb.setText("$cur_life/$hero_max")
            mtextb.setText("$m1g1_life/$max_gwlife")
            val cur_finish = RandomSkill().finish(this)
            if (cur_finish>0){
                if (cur_finish==3){
                    LoadAndSave().save("true","1",this)
                    //完成挑战，随机爆装备
                    m1start.setText("完成")
                    m1exit.setText("退出")
                    if (GetEquip().isGetEquip()){
                        //随机装备
                        UserDatabaseUtil().addexp(this,cur_acc,exp)
                        val ename = GetEquip().randomEquip(this,1)
                        val e_name = ename[0]
                        val e_id = ename[1]
                        val activity = this as Activity
                        AlertDialog.Builder(this).apply {
                            setTitle("胜利")
                            setMessage("恭喜你额外获得了装备: $e_name,请选择是否穿戴")
                            setNegativeButton("丢弃"){_,_->
                                activity.finish()
                            }
                            setPositiveButton("装备"){_,_->
                                AllEquipDatabaseUtil().update(activity,e_id,cur_acc)
                                activity.finish()
                            }
                            show()
                        }
                    }
                    else{
                        UserDatabaseUtil().addexp(this,cur_acc,exp)
                        val activity = this as Activity
                        AlertDialog.Builder(this).apply {
                            setTitle("胜利")
                            setMessage("恭喜你完成挑战，并获得${exp}经验值,点击退出回到主页！")
                            setPositiveButton("退出"){_,_->
                                activity.finish()
                            }
                            show()
                        }
                    }
                }
                else{
                    when(cur_finish){
                        1 -> LoadAndSave().save("m1g2","cur_fight",this)
                        2 -> LoadAndSave().save("m1g3","cur_fight",this)
                    }
                    m1start.setText("继续")
                }
                //打败怪物
                m1start.isClickable = true
                m1start.isEnabled = true

                m1start.setTextColor(Color.parseColor("#2085D5"))
                map1gw.visibility = View.INVISIBLE
                m1gprog.visibility = View.INVISIBLE
                m1gb.setText("")
                mtextb.setText("")
                map1card1.visibility = View.INVISIBLE
                map1card2.visibility = View.INVISIBLE
                map1card3.visibility = View.INVISIBLE

            }
            else{
                map1card1.isClickable=false
                map1card1.isEnabled=false
                map1card2.isClickable=false
                map1card2.isEnabled=false
                map1card3.isClickable=false
                map1card3.isEnabled=false
                map1card2.setBackgroundResource(R.drawable.suo)
                map1card3.setBackgroundResource(R.drawable.suo)
                map1card2.setText("")
                map1card3.setText("")

            }
            if (LoadAndSave().load(cur_fight,this).equals("true")){
                Handler().postDelayed({
                    mfinished()
                },1100)
            }

//            map1card1.isClickable=false
//            map1card1.isEnabled=false
//            map1card2.isClickable=false
//            map1card2.isEnabled=false
//            map1card3.isClickable=false
//            map1card3.isEnabled=false
//            map1card2.setBackgroundResource(R.drawable.suo)
//            map1card3.setBackgroundResource(R.drawable.suo)
//            map1card2.setText("")
//            map1card3.setText("")
        }
        map1card2.setOnClickListener {
            map1card2.visibility = View.INVISIBLE
            val res1 = RandomSkill().action(2,this)
            if (res1<0){
                m1hurt.setText(res1.toString())
                Handler().postDelayed({
                    m1hurt.setText("")
                },400)
            }
            else{
                m1heroadd.setText("+$res1")
                Handler().postDelayed({
                    m1heroadd.setText("")
                },400)
            }

            val cur_fight = LoadAndSave().load("cur_fight",this)
            cur_life = LoadAndSave().load("hlife",this)
            m1g1_life = LoadAndSave().load(cur_fight+"life",this)
            m1gprog.progress = (m1g1_life.toInt()*100)/max_gwlife
//            hprog.progress = (cur_life.toInt()*100)/(LoadAndSave().load("maxlife",this).toInt())
            hprog.progress = (cur_life.toInt()*100)/(LoadAndSave().load("maxlife",this).toInt())
            htextb.setText("$cur_life/$hero_max")
            mtextb.setText("$m1g1_life/$max_gwlife")
            val cur_finish = RandomSkill().finish(this)
            if (cur_finish>0){
                if (cur_finish==3){
                    LoadAndSave().save("true","1",this)
                    //完成挑战，随机爆装备
                    m1start.setText("完成")
                    m1exit.setText("退出")
                    if (GetEquip().isGetEquip()){
                        //随机装备
                        UserDatabaseUtil().addexp(this,cur_acc,exp)
                        val ename = GetEquip().randomEquip(this,1)
                        val e_name = ename[0]
                        val e_id = ename[1]
                        val activity = this as Activity
                        AlertDialog.Builder(this).apply {
                            setTitle("胜利")
                            setMessage("恭喜你额外获得了装备: $e_name,请选择是否穿戴")
                            setNegativeButton("丢弃"){_,_->
                                activity.finish()
                            }
                            setPositiveButton("装备"){_,_->
                                AllEquipDatabaseUtil().update(activity,e_id,cur_acc)
                                activity.finish()
                            }
                            show()
                        }
                    }
                    else{
                        UserDatabaseUtil().addexp(this,cur_acc,exp)
                        val activity = this as Activity
                        AlertDialog.Builder(this).apply {
                            setTitle("胜利")
                            setMessage("恭喜你完成挑战，并获得${exp}经验值,点击退出回到主页！")
                            setPositiveButton("退出"){_,_->
                                activity.finish()
                            }
                            show()
                        }
                    }
                }
                else{
                    when(cur_finish){
                        1 -> LoadAndSave().save("m1g2","cur_fight",this)
                        2 -> LoadAndSave().save("m1g3","cur_fight",this)
                    }
                    m1start.setText("继续")
                }
                //打败怪物
                m1start.isClickable = true
                m1start.isEnabled = true

                m1start.setTextColor(Color.parseColor("#2085D5"))
                map1gw.visibility = View.INVISIBLE
                m1gprog.visibility = View.INVISIBLE
                m1gb.setText("")
                mtextb.setText("")
                map1card1.visibility = View.INVISIBLE
                map1card2.visibility = View.INVISIBLE
                map1card3.visibility = View.INVISIBLE
            }
            else{
                map1card1.isClickable=false
                map1card1.isEnabled=false
                map1card2.isClickable=false
                map1card2.isEnabled=false
                map1card3.isClickable=false
                map1card3.isEnabled=false
                map1card1.setBackgroundResource(R.drawable.suo)
                map1card3.setBackgroundResource(R.drawable.suo)
                map1card1.setText("")
                map1card3.setText("")
            }
            if (LoadAndSave().load(cur_fight,this).equals("true")){
                Handler().postDelayed({
                    mfinished()
                },1100)
            }
//            map1card1.isClickable=false
//            map1card1.isEnabled=false
//            map1card2.isClickable=false
//            map1card2.isEnabled=false
//            map1card3.isClickable=false
//            map1card3.isEnabled=false
//            map1card1.setBackgroundResource(R.drawable.suo)
//            map1card3.setBackgroundResource(R.drawable.suo)
//            map1card1.setText("")
//            map1card3.setText("")
        }
        map1card3.setOnClickListener {
            map1card3.visibility = View.INVISIBLE
            val res1 = RandomSkill().action(3,this)
            if (res1<0){
                m1hurt.setText(res1.toString())
                Handler().postDelayed({
                    m1hurt.setText("")
                },400)
            }
            else{
                m1heroadd.setText("+$res1")
                Handler().postDelayed({
                    m1heroadd.setText("")
                },400)
            }

            val cur_fight = LoadAndSave().load("cur_fight",this)
            cur_life = LoadAndSave().load("hlife",this)
            m1g1_life = LoadAndSave().load(cur_fight+"life",this)
            m1gprog.progress = (m1g1_life.toInt()*100)/max_gwlife
//            hprog.progress = (cur_life.toInt()*100)/(LoadAndSave().load("maxlife",this).toInt())
            hprog.progress = (cur_life.toInt()*100)/(LoadAndSave().load("maxlife",this).toInt())
            htextb.setText("$cur_life/$hero_max")
            mtextb.setText("$m1g1_life/$max_gwlife")
            val cur_finish = RandomSkill().finish(this)
            if (cur_finish>0){
                if (cur_finish==3){
                    LoadAndSave().save("true","1",this)
                    //完成挑战，随机爆装备
                    m1start.setText("完成")
                    m1exit.setText("退出")
                    if (GetEquip().isGetEquip()){
                        //随机装备
                        UserDatabaseUtil().addexp(this,cur_acc,exp)
                        val ename = GetEquip().randomEquip(this,1)
                        val e_name = ename[0]
                        val e_id = ename[1]
                        val activity = this as Activity
                        AlertDialog.Builder(this).apply {
                            setTitle("胜利")
                            setMessage("恭喜你额外获得了装备: $e_name,请选择是否穿戴")
                            setNegativeButton("丢弃"){_,_->
                                activity.finish()
                            }
                            setPositiveButton("装备"){_,_->
                                AllEquipDatabaseUtil().update(activity,e_id,cur_acc)
                                activity.finish()
                            }
                            show()
                        }
                    }
                    else{
                        UserDatabaseUtil().addexp(this,cur_acc,exp)
                        val activity = this as Activity
                        AlertDialog.Builder(this).apply {
                            setTitle("胜利")
                            setMessage("恭喜你完成挑战，并获得${exp}经验值,点击退出回到主页！")
                            setPositiveButton("退出"){_,_->
                                activity.finish()
                            }
                            show()
                        }
                    }
                }
                else{
                    when(cur_finish){
                        1 -> LoadAndSave().save("m1g2","cur_fight",this)
                        2 -> LoadAndSave().save("m1g3","cur_fight",this)
                    }
                    m1start.setText("继续")
                }
                //打败怪物
                m1start.isClickable = true
                m1start.isEnabled = true
//                m1finish.isClickable = false
//                m1finish.setTextColor(Color.parseColor("#919AA2"))
                m1start.setTextColor(Color.parseColor("#2085D5"))
                map1gw.visibility = View.INVISIBLE
                m1gprog.visibility = View.INVISIBLE
                m1gb.setText("")
                mtextb.setText("")
                map1card1.visibility = View.INVISIBLE
                map1card2.visibility = View.INVISIBLE
                map1card3.visibility = View.INVISIBLE
            }
            else{
                map1card1.isClickable=false
                map1card1.isEnabled=false
                map1card2.isClickable=false
                map1card2.isEnabled=false
                map1card3.isClickable=false
                map1card3.isEnabled=false
                map1card2.setBackgroundResource(R.drawable.suo)
                map1card1.setBackgroundResource(R.drawable.suo)
                map1card2.setText("")
                map1card1.setText("")
            }
            if (LoadAndSave().load(cur_fight,this).equals("true")){
                Handler().postDelayed({
                    mfinished()
                },1100)
            }
//            map1card1.isClickable=false
//            map1card1.isEnabled=false
//            map1card2.isClickable=false
//            map1card2.isEnabled=false
//            map1card3.isClickable=false
//            map1card3.isEnabled=false
//            map1card2.setBackgroundResource(R.drawable.suo)
//            map1card1.setBackgroundResource(R.drawable.suo)
//            map1card2.setText("")
//            map1card1.setText("")
        }

        m1exit.setOnClickListener {
            val activity = this as Activity
            activity.finish()
        }
    }
    fun mfinished(){
        val max_gwlife = LoadAndSave().load("max_gwlife",this).toInt()
        val m_res = RandomSkill().mattack(this,1)
        val cur_life = LoadAndSave().load("hlife",this)
        Handler().postDelayed({
            //                hprog.progress = (cur_life.toInt()*100)/(LoadAndSave().load("maxlife",this).toInt())
            hprog.progress = (cur_life.toInt()*100)/(LoadAndSave().load("maxlife",this).toInt())
        },400)
        Handler().postDelayed({
            val cur_skill = RandomSkill().showSkill(this)
            map1card1.setText(cur_skill[0])
            map1card1.setTextColor(Color.parseColor(cur_skill[1]))
            map1card2.setText(cur_skill[2])
            map1card2.setTextColor(Color.parseColor(cur_skill[3]))
            map1card3.setText(cur_skill[4])
            map1card3.setTextColor(Color.parseColor(cur_skill[5]))
            map1card2.setBackgroundResource(R.drawable.card)
            map1card1.setBackgroundResource(R.drawable.card)
            map1card3.setBackgroundResource(R.drawable.card)
            map1card1.visibility = View.VISIBLE
            map1card2.visibility = View.VISIBLE
            map1card3.visibility = View.VISIBLE
        },1000)
        val temp = m_res[1]
        m1heroadd.setText("-$temp")
        Handler().postDelayed({
            m1heroadd.setText("")
        },400)

        if (m_res[0]==0){
            val activity = this as Activity
            AlertDialog.Builder(this).apply {
                setTitle("挑战失败")
                setMessage("生命值为0，无法继续挑战！")
                setPositiveButton("退出"){_,_->
                    activity.finish()
                }
                show()
            }
        }

        map1card1.isClickable=true
        map1card1.isEnabled=true
        map1card2.isClickable=true
        map1card2.isEnabled=true
        map1card3.isClickable=true
        map1card3.isEnabled=true
//            m1start.isClickable = true
//            m1start.isEnabled = true
        val flagm1g1 = LoadAndSave().load("m1g1",this)
        val flagm1g2 = LoadAndSave().load("m1g2",this)
        val flagm1g3 = LoadAndSave().load("m1g3",this)

        val hero_max = LoadAndSave().load("maxlife",this)
        val cur_fight = LoadAndSave().load("cur_fight",this)
        val m1g1_life = LoadAndSave().load(cur_fight+"life",this)

        htextb.setText("$cur_life/$hero_max")
        if (LoadAndSave().load(cur_fight,this).equals("true")){
            mtextb.setText("$m1g1_life/$max_gwlife")
        }

    }
}
